package com.ufinity.estimation.service.impl;

import java.util.List;

import org.springframework.dao.DataAccessException;

import com.cn.ufinity.common.log.Logger;
import com.cn.ufinity.common.log.LoggerFactory;

import com.ufinity.estimation.persistence.ServiceException;
import com.ufinity.estimation.persistence.domain.HouseStructureTemplate;
import com.ufinity.estimation.persistence.domain.User;
import com.ufinity.estimation.service.UserService;

public class UserServiceImpl extends BaseServiceImpl<User, Long> implements
		UserService {

	 public UserServiceImpl() {
	        super(LoggerFactory
	                .getPersistenceLog(UserServiceImpl.class),
	                User.class);
	    }


	@SuppressWarnings("unchecked")
	@Override
	public User getUserByUserName(String userName) throws ServiceException {
		String method = "getUserByUserName";
		logger.info(method,
				"get user from database by username, the username is:"
						+ userName);
		String hql = "from User as u where u.username = ?";
		User user = null;

		try {
			List<User> userList = getHibernateTemplate().find(hql,
					new Object[] { userName });
			if (!userList.isEmpty()) {
				user = (User) userList.get(0);
			}
			return user;
		} catch (DataAccessException e) {
			String errorMessage = "an error occurs during find user by username: ";
			logger.error(method, errorMessage + e);
			throw new ServiceException(errorMessage, e);
		}
	}


}
